package com.topn;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

// 排序比较器
public class TSortComparator extends WritableComparator {


    // 针对的是哪个数据类型来排序， TKey
    public TSortComparator() {
        super(Tkey.class, true);
    }


    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        Tkey k1 = (Tkey) a;
        Tkey k2 = (Tkey) b;
        // sort:  年，月，温度  且 温度倒序(排序逻辑可以写在key中)
        // 比较年
        int yCompare = Integer.compare(k1.getYear(), k2.getYear());

        if (yCompare == 0) {
            // 比较月
            int mCompare = Integer.compare(k1.getMonth(), k2.getMonth());
            if (mCompare == 0) {
                // 温度倒序
                return -Integer.compare(k1.getTemp(), k2.getTemp());
            }
            return mCompare;
        }

        return yCompare;


    }
}